package com.szrt.day20170504.ch04;

public class TicketThread implements Runnable {
	
	/**
	 * 一共有一百张票
	 * 
	 * 100 99 98 ...1 
	 * 动态的属性 跟对象走
	 */
	private  int  ticket =100;

	@Override
	public void run() {
		/**
		 * 可能1个窗口卖100张票  其他的窗口一张票都没卖  
		 */
		for(int index =1; index<=100;index++){
			
			synchronized (this) {
				if (ticket >= 1) {
					try {
						System.out.println(Thread.currentThread().getName() + "开始买票：");
						Thread.currentThread().sleep(50);
						System.out.println(Thread.currentThread().getName() + "票号=" + ticket--);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
			
		}
		
	}

}
